package com.dimples.controller.system;

import com.dimples.common.annotation.OpsLog;
import com.dimples.common.enums.LoggingTypeEnum;
import com.dimples.common.enums.OpsLogTypeEnum;
import com.dimples.common.page.metadata.Page;
import com.dimples.common.transport.QueryRequest;
import com.dimples.common.transport.Result;
import com.dimples.controller.view.BaseWebController;
import com.dimples.system.entity.Logging;
import com.dimples.system.service.LoggingService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/6/26
 */
@Slf4j
@Api(tags = "日志管理")
@Validated
@RestController
@RequestMapping(value = BaseWebController.API_SYSTEM_PREFIX)
@RequiredArgsConstructor
public class LoggingController {

    private final LoggingService loggingService;

    @ApiOperation(value = "获取操作日志")
    @ApiOperationSupport(order = 1)
    @OpsLog(value = "获取操作日志", type = OpsLogTypeEnum.SELECT)
    @GetMapping("log/operate")
    @PreAuthorize("@auth.hasPerm('log:opera:view')")
    public Result<Logging> findOperaLog(Logging logging, QueryRequest queryRequest) {
        logging.setLoggingType(LoggingTypeEnum.OPERATE.name());
        Page<Logging> page = this.loggingService.findLog(logging, queryRequest);
        return Result.success(page);
    }

    @ApiOperation(value = "获取登陆日志")
    @ApiOperationSupport(order = 3)
    @OpsLog(value = "获取登陆日志", type = OpsLogTypeEnum.SELECT)
    @GetMapping("log/login")
    @PreAuthorize("@auth.hasPerm('log:login:view')")
    public Result<Logging> findLoginLog(Logging logging, QueryRequest queryRequest) {
        logging.setLoggingType(LoggingTypeEnum.LOGIN.name());
        Page<Logging> page = this.loggingService.findLog(logging, queryRequest);
        return Result.success(page);
    }

}
